/**
 * 
 */
package com.yidu.menuProject.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.plaf.synth.SynthSpinnerUI;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yidu.menuProject.action.vo.EhyManagerRoleVo;
import com.yidu.menuProject.action.vo.EhyManagerVo;
import com.yidu.menuProject.action.vo.EhyModuleVo;
import com.yidu.menuProject.action.vo.EhyRoleVo;
import com.yidu.menuProject.commont.Tools;
import com.yidu.menuProject.dao.EhyManagerModelMapper;
import com.yidu.menuProject.dao.EhyManagerRoleModelMapper;
import com.yidu.menuProject.dao.EhyModuleModelMapper;
import com.yidu.menuProject.model.EhyManagerModel;
import com.yidu.menuProject.model.EhyManagerRoleModel;
import com.yidu.menuProject.model.EhyModuleModel;
import com.yidu.menuProject.model.EhyRoleModel;




/**
 * 客户的服务类
 * @author liandyao
 * @date 2018年6月8日
 * @version 1.0
 */

@Service
public class EhyManagerServiceImpl implements EhyManagerService{


	@Resource
	private EhyManagerModelMapper managerDao;
	@Resource
	private EhyManagerRoleModelMapper managerRoleDao;
	@Resource
	private EhyModuleModelMapper moduleDao;
	
	@Override
	public int deleteByPrimaryKey(String manId) {
		
		return managerDao.deleteByPrimaryKey(manId);
	}

	@Override
	public int insert(EhyManagerModel record) {
		return managerDao.insert(record);
	}

	@Override
	public int insertSelective(EhyManagerVo vo) {
		managerDao.insertSelective(vo);
		String[] role=vo.getCheckName().split("_");
		for (String string : role) {
			EhyManagerRoleModel mr=new EhyManagerRoleModel();
			mr.setMrId(Tools.getRandomString(20));
			mr.setRoleId(string);
			mr.setManId(vo.getManId());
			managerRoleDao.insertSelective(mr);
		}
		System.out.println("11111111================="+vo.getCheckName());
		return 1;
	}

	@Override
	public EhyManagerModel selectByPrimaryKey(String manId) {
		
		return managerDao.selectByPrimaryKey(manId);
	}

	@Override
	public int updateByPrimaryKeySelective(EhyManagerVo vo) {
		
		managerDao.updateByPrimaryKeySelective(vo);
		
		String[] role=vo.getCheckName().split("_");
		for (String string : role) {
			EhyManagerRoleModel mr=new EhyManagerRoleModel();
			managerRoleDao.deleteMan(mr.getManId());
			mr.setMrId(Tools.getRandomString(20));
			mr.setRoleId(string);
			mr.setManId(vo.getManId());
			managerRoleDao.insertSelective(mr);
		}
		return 1;
	}

	@Override
	public int updateByPrimaryKey(EhyManagerModel record) {
		
		return managerDao.updateByPrimaryKey(record);
	}


	@Override
	public List<EhyManagerModel> findModule(EhyManagerVo vo) {
		
		return managerDao.findModule(vo);
	}

	
	

	@Override
	public int selectCount(EhyManagerVo vo) {
	
		return managerDao.selectCount(vo);
	}

	@Override
	public List<EhyManagerModel> selectAll(EhyManagerVo vo) {
		
		return managerDao.selectAll(vo);
	}

	@Override
	public EhyManagerModel showUpdate(String id) {
		
		return managerDao.showUpdate(id);
	}

	@Override
	public void saveOrUpdate(EhyManagerModel po) {
		
		
	}

	@Override
	public int delete(String id) {
	
		return managerDao.delete(id);
	}

	@Override
	public EhyManagerVo findById(String id) {
		EhyManagerModel po = new EhyManagerModel();
		EhyManagerVo vo = new EhyManagerVo();
		po=managerDao.findById(id);
		BeanUtils.copyProperties(po, vo);
		return vo;
	}

	@Override
	public List<EhyModuleVo> findModule(String manUser, String manPwd) {

		List<EhyModuleModel> list= moduleDao.findModule(manUser, manPwd);
		List<EhyModuleVo> listvo = new ArrayList<>();
		
		for(EhyModuleModel module :list ){
			EhyModuleVo voo=new EhyModuleVo();
			BeanUtils.copyProperties(module, voo);
			listvo.add(voo);
		}
		return listvo;
	}

	@Override
	public EhyManagerVo findCustomer(String manUser, String manPwd) {
		EhyManagerModel po = managerDao.findCustomer(manUser, manPwd);
		if(po!=null){
			EhyManagerVo vo = new EhyManagerVo(); 
			//spring的包copy方法
			BeanUtils.copyProperties(po, vo); 
			return vo ;
		}else{
			return null ;
		}
	}
	/**
	 * 判断是否登录
	 */
	@Override
	public EhyManagerModel isLogin(String manUser, String manPwd) {
		EhyManagerModel ehymanager = managerDao.isLogin(manUser, manPwd);
		return ehymanager;
	}

}

